package com.RequestGongWuShu;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.File;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 施工升降机对接测试 注：方法上的注释打√的方法为对接测试环境已通过
 * 
 * @author YS
 * @date 2020/12/04 16:58
 */

@Slf4j
public class elevator {

	private String api_key = "1cc9912ee3944159953a5eca383ea3fd";
	private String api_version = "1.0";
	private String engCode = "GDSYEQ-010-2019";// 工程编号
	private String projectCode = "XM20170281";// 项目编号
	private String baseUlr = "https://szwb.sz.gov.cn:2018/openapi";// 请求地址

	/**
	 * 2.4.12.4.1上报升降机基本信息 √
	 */
	public void addBasicInfo() {
		String url = baseUlr + "/elevator/addBasicInfo";
		String timestamp = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");

		JSONObject json = new JSONObject();
		json.put("device_no", "7986207e559b45a3ab62611c2888222b");
		json.put("model", "model-s");
		json.put("name", "15#升降机");
		json.put("uid", "设备用户编号");
		json.put("license", "设备证书日期");
		json.put("license_start_date", "设备发证日期");
		json.put("license_end_date", "设备有效日期");
//		json.put("photos", 10101);  //好像对方根本没这个接口，填任何类型都报错
		json.put("property_unit", "设备产权单位");
		json.put("mnufacturer_business", "设备制造商");
		json.put("mai_war_bus", "设备维保商");
		json.put("testing_business", "设备检测商");
		json.put("in_date", "设备进场时间");
		json.put("out_date", "设备出场时间");
		json.put("mon_device_man", "监测设备厂商");
		json.put("device_installation_date", "监测设备安装日期");
		json.put("guangdong_install_num", "广东省统一安装告知编号（产权备案编号）例如:粤BE-S03981");

		String body = JSON.toJSONString(json);

		String signature = getSignature(api_key, api_version, engCode, projectCode, timestamp, body);
		Map<String, Object> param = new HashMap<>();
		param.put("api_key", api_key);
		param.put("project_code", projectCode);
		param.put("eng_code", engCode);
		param.put("api_version", api_version);
		param.put("timestamp", timestamp);
		param.put("body", json);
		param.put("signature", signature);
		JSONObject json2 = new JSONObject(param);
		String a = json2.toJSONString();

		log.info("All:" + a);
		String info = HttpUtil.post(url, a);
		log.info("down!" + info);
	}

	/**
	 * 2.4.12.4.1上报升降机基本信息 √
	 */
	public void addParams() {
		String url = baseUlr + "/elevator/addParams";
		String timestamp = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");

		JSONObject json = new JSONObject();
		json.put("device_no", "7986207e559b45a3ab62611c28882211");
		json.put("model", "model-s");
		json.put("name", "14#升降机");
		json.put("L_Load_Capacity", 1.1);
		json.put("L_Height", 1.1);
		json.put("L_Height2", 1.1);
		json.put("L_UpCollect", 1);
		json.put("L_DownCollection", 1);

		json.put("L_GearModules", 1);
		json.put("L_LowLimit", 1);
		json.put("L_M_Contract", 1);
		json.put("L_S_Contract", 1);
		json.put("L_MonitorStyle", 1);
		json.put("L_Sections", 1);
		json.put("L_ControlStyle", 1);
		json.put("L_Limit_Capacity", 2.3);

		String body = JSON.toJSONString(json);

		String signature = getSignature(api_key, api_version, engCode, projectCode, timestamp, body);
		Map<String, Object> param = new HashMap<>();
		param.put("api_key", api_key);
		param.put("project_code", projectCode);
		param.put("eng_code", engCode);
		param.put("api_version", api_version);
		param.put("timestamp", timestamp);
		param.put("body", json);
		param.put("signature", signature);
		JSONObject json2 = new JSONObject(param);
		String a = json2.toJSONString();

		log.info("All:" + a);
		String info = HttpUtil.post(url, a);
		log.info("down!" + info);
	}

	/**
	 * 2.4.5上报升降机通电时间接口√ array
	 */
	public void electrify() {
		String url = baseUlr + "/elevator/electrify";
		String timestamp = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");

		JSONObject param = new JSONObject();
		JSONObject body = new JSONObject();
		param.put("api_key", api_key);
		param.put("project_code", projectCode);
		param.put("eng_code", engCode);
		param.put("api_version", api_version);
		param.put("timestamp", timestamp);

		body.put("device_no", "7986207e559b45a3ab62611c28882211");
		body.put("runtime", "2020-12-04 17:02:59");
		body.put("operation", 0);

		List<JSONObject> bodyList = new ArrayList<>();// 可以传入多个body
		bodyList.add(body);
		param.put("body", bodyList);// body

		String jsonBody = JSONObject.toJSONString(bodyList);// body的json字符串
		log.info(jsonBody);
		String signature = getSignature(api_key, api_version, engCode, projectCode, timestamp, jsonBody);
		param.put("signature", signature);

		String info = HttpUtil.post(url, param.toJSONString());
		log.info(info);
	}

	/**
	 * 2.4.6上报升降机司机打卡记录信息√ array
	 */
	public void clockIn() {
		String url = baseUlr + "/elevator/clockIn";
		String timestamp = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");

		JSONObject param = new JSONObject();
		JSONObject body = new JSONObject();
		param.put("api_key", api_key);
		param.put("project_code", projectCode);
		param.put("eng_code", engCode);
		param.put("api_version", api_version);
		param.put("timestamp", timestamp);

		body.put("device_no", "7986207e559b45a3ab62611c28882233");
		body.put("worker_name", "一顺小瓜");
		body.put("start_time", "2020-12-07 08:12:59");
		body.put("end_time", "2020-12-07 17:02:59");

		List<JSONObject> bodyList = new ArrayList<>();// 可以传入多个body
		bodyList.add(body);
		param.put("body", bodyList);// body

		String jsonBody = JSONObject.toJSONString(bodyList);// body的json字符串
		log.info(jsonBody);
		String signature = getSignature(api_key, api_version, engCode, projectCode, timestamp, jsonBody);
		param.put("signature", signature);

		String info = HttpUtil.post(url, param.toJSONString());
		log.info(info);
	}

	/**
	 * 2.4.4上报升降机操作记录 √ array
	 */
	public void operator() {
		String url = baseUlr + "/elevator/operator";
		String timestamp = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");

		JSONObject param = new JSONObject();
		JSONObject body = new JSONObject();
		param.put("api_key", api_key);
		param.put("project_code", projectCode);
		param.put("eng_code", engCode);
		param.put("api_version", api_version);
		param.put("timestamp", timestamp);

		body.put("device_no", "7986207e559b45a3ab62611c28882233");
		body.put("start_time", "2020-12-07 08:12:59");
		body.put("end_time", "2020-12-07 17:02:59");
		body.put("direction", 1);
		body.put("max_load_value", 1.0);
		body.put("is_over_load", 1);
		body.put("operator_name", "持卡人签名");
		body.put("isIllegal_operation", 1);

		List<JSONObject> bodyList = new ArrayList<>();// 可以传入多个body
		bodyList.add(body);
		param.put("body", bodyList);// body

		String jsonBody = JSONObject.toJSONString(bodyList);// body的json字符串
		log.info(jsonBody);
		String signature = getSignature(api_key, api_version, engCode, projectCode, timestamp, jsonBody);
		param.put("signature", signature);

		String info = HttpUtil.post(url, param.toJSONString());
		log.info(info);
	}

	/**
	 * 2.4.4上报升降机操作记录 √ array
	 */
	public void addRecord() {
		String url = baseUlr + "/elevator/addRecord";
		String timestamp = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");

		JSONObject param = new JSONObject();
		JSONObject body = new JSONObject();
		param.put("api_key", api_key);
		param.put("project_code", projectCode);
		param.put("eng_code", engCode);
		param.put("api_version", api_version);
		param.put("timestamp", timestamp);

		body.put("device_no", "7986207e559b45a3ab62611c28882233");
		body.put("runtime", "2020-12-07 08:12:59");
		body.put("load", 1.0);
		body.put("load_ratio", 1.0);
		body.put("sign", 1);
		body.put("loading_capacity", 1);
		body.put("operator_name", "小发");
		body.put("ic_num", 1);
		body.put("direction", 1);
		body.put("height", 1.0);
		body.put("real_time_flag", 1);
		body.put("status", 1);
		body.put("braking_distance", 1);
		body.put("is_up_warning", 1);
		body.put("is_down_warning", 1);
		body.put("is_safety_device_warn", 0);
		body.put("is_over_warning", 0);
		body.put("is_forward_warning", 0);
		body.put("is_backward_warning", 0);
		body.put("is_limit_warning", 0);

		List<JSONObject> bodyList = new ArrayList<>();// 可以传入多个body
		bodyList.add(body);
		param.put("body", bodyList);// body

		String jsonBody = JSONObject.toJSONString(bodyList);// body的json字符串
		log.info(jsonBody);
		String signature = getSignature(api_key, api_version, engCode, projectCode, timestamp, jsonBody);
		param.put("signature", signature);

		String info = HttpUtil.post(url, param.toJSONString());
		log.info(info);
	}

	/**
	 * 硬件通用上传图片√
	 */
	public void uploadImage() {
		HashMap<String, Object> paramMap = new HashMap<>();
		// 文件
		File file = new File("C:\\Users\\YS\\Desktop\\downloadImgToWebSiteWeiChat\\timg.jpg");
		paramMap.put("file", file);
		paramMap.put("api_key", api_key);
		String result = HttpUtil.post("https://szwb.sz.gov.cn:2019/uploadfile-openpai/uploadImage", paramMap);
		log.info(result);// 返回文件的id
		// resultData":"ffbe6ac3b17f5a457cbd1a7abfe2ff1e"
	}

	/**
	 * 获取签名
	 *
	 * @param: [apiKey, apiVersion, engCode, projectCode, timestamp, body]
	 * @return: java.lang.String
	 */
	public static String getSignature(String apiKey, String apiVersion, String engCode, String projectCode,
			String timestamp, String body) {
		String str4MD5 = String.format("api_key%sapi_version%sbody%seng_code=%sproject_code=%stimestamp%s", apiKey,
				apiVersion, body.isEmpty() ? "{}" : body, engCode, projectCode, timestamp);
		return getMD5Code(str4MD5);
	}

	public static String getMD5Code(String strObj) {
		String resultString = null;
		try {
			resultString = new String(strObj);
			MessageDigest md = MessageDigest.getInstance("MD5");
			Charset charset = Charset.forName("UTF-8");
			resultString = byteToString(md.digest(strObj.getBytes(charset)));
		} catch (NoSuchAlgorithmException ex) {
			ex.printStackTrace();
		}
		return resultString;
	}

	// 转换字节数组为16进制字串
	private static String byteToString(byte[] bByte) {
		StringBuffer sBuffer = new StringBuffer();
		for (int i = 0; i < bByte.length; i++) {
			sBuffer.append(byteToArrayString(bByte[i]));
		}
		return sBuffer.toString();
	}

	private static String byteToArrayString(byte bByte) {
		int iRet = bByte;
		if (iRet < 0) {
			iRet += 256;
		}
		int iD1 = iRet / 16;
		int iD2 = iRet % 16;
		return strDigits[iD1] + strDigits[iD2];
	}

	// 全局数组
	private final static String[] strDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D",
			"E", "F" };

}
